package org.mtdownload.infoview;

import javax.swing.table.DefaultTableModel;

import org.mtdownload.download.DownloadTask;

public class InfoViewTableModel extends DefaultTableModel {
	
	private static final long serialVersionUID = 3832906567585379892L;
	private static final String[] rows=new String[]{"File","Status","Percent","Speed","Average speed","Completed/Total","ETA","Elapsed","Threads","URL"};
	private static final String[] colNames=new String[]{"Property name","Property value"};
	

	public InfoViewTableModel() {
		super(colNames, 0);
		init();
	}
	
	private void init(){
		for(int i=0;i<rows.length;i++){
			addRow(new String[]{rows[i],""});
		}
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int)
	 */
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#getColumnCount()
	 */
	public int getColumnCount() {
		return colNames.length;
	}
	
	public void updateData(DownloadTask task){
		if(task!=null){
			setValueAt(task.getDestinationFileName(),0,1);
			setValueAt(task.getStatus(),1,1);
			setValueAt(task.getHumanFriendlyPercent(),2,1);
			setValueAt(task.getHumanFriendlySpeed(DownloadTask.SPEED_KILOBYTES) + " KB/s",3,1);
			if(task.getElapsed()>0){
				long averageSpeed=task.getCompletedBytes()/task.getElapsed();
				 setValueAt(DownloadTask.getHumanFriendlySpeed(DownloadTask.SPEED_KILOBYTES,averageSpeed) + " KB/s",4,1);
			}else{
				setValueAt("",4,1);
			}			
			setValueAt(task.getHumanFriendlyBytes(task.getCompletedBytes())+"/"+task.getHumanFriendlyBytes(task.getFileLength()),5,1);
			setValueAt(task.getHumanFriendlyTime(task.getEta()),6,1);
			setValueAt(task.getHumanFriendlyTime(task.getElapsed()),7,1);
			setValueAt( task.getThreadNum() + "",8,1);
			setValueAt(task.getUrl(),9,1);
		}else{
			setValueAt("",0,1);
			setValueAt("",1,1);
			setValueAt("",2,1);
			setValueAt("",3,1);
			setValueAt("",4,1);
			setValueAt("",5,1);
			setValueAt("",6,1);
			setValueAt("",7,1);
			setValueAt("",8,1);
		}
		fireTableRowsUpdated(0,rows.length);		
	}
}
